<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.geqian.web.mapper.${className}Mapper">

    <resultMap id="BaseResultMap" type="com.geqian.web.entity.${className}">
    <#if dataList??>
    <#list dataList as data>
        <#if data.primaryKey>
            <id property="${data.fieldName}" column="${data.columnName}" jdbcType="${data.jdbcType}"/>
        <#else>
            <result property="${data.fieldName}" column="${data.columnName}" jdbcType="${data.jdbcType}"/>
        </#if>
    </#list>
    </#if>
    </resultMap>

    <sql id="Base_Column_List">
    <#if dataList??>
        <#list dataList as data>
            <#if !data.autoIncrement>
            `${data.columnName}`<#if data?index != (dataList?size - 1)>,</#if>
            </#if>
        </#list>
    </#if>
    </sql>


    <insert id="insertBatch">
        insert into ${tableName} (<include refid="Base_Column_List"></include>) values
        <foreach item="item" index="index" collection="list" separator=",">
            (<#if dataList??><#list dataList as data><#if !data.autoIncrement>${'#{' + 'item.' + data.fieldName + '}' }<#if data?index != (dataList?size - 1)>,</#if></#if></#list></#if>)
        </foreach>
    </insert>


    <delete id="deleteBatch">
        delete from ${tableName}
        where (<#list keySet as entry>${entry.key}<#if entry?index != (keySet?size - 1)>,</#if></#list>) in
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            (<#list keySet as entry>${'#{' + 'item.' + entry.value + '}' }<#if entry?index != (keySet?size - 1)>,</#if></#list>)
        </foreach>
    </delete>

    <update id="updateBatch" parameterType="java.util.List">
        <foreach collection="list" item="item" separator=";">
            update ${tableName}
            <set>
            <#if dataList??>
                <#list dataList as data>
                <#if !data.autoIncrement>
                <if test="item.${data.fieldName} != null">
                    ${data.columnName}=${'#{' + 'item.' + data.fieldName + '}' }<#if data?index != (dataList?size - 1)>,</#if>
                </if>
                </#if>
                </#list>
            </#if>
            </set>
            where <#list keySet as entry>${entry.key} = ${'#{' + 'item.' + entry.value + '}' }<#if entry?index != (keySet?size - 1)> and </#if></#list>
        </foreach>
    </update>



    <select id="existConflictKeys" resultType="boolean">
        select case count(*) when 0 then false else true end
        from ${tableName}
        where (<#list keySet as entry>${entry.key}<#if entry?index != (keySet?size - 1)>,</#if></#list>) in
        ((<#list keySet as entry>${'#{'+ entry.value + '}'}<#if entry?index != (keySet?size - 1)>, </#if></#list>))
    </select>

</mapper>


